package com.example.demo.controller;


import com.example.demo.entity.Constantitem;
import com.example.demo.entity.Constanttype;
import com.example.demo.entity.Drugs;
import com.example.demo.service.BConstantitemService;
import com.example.demo.service.BConstanttypeService;
import com.example.demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZQW
 */
@Controller
@RequestMapping("/constantitem")
public class BConstantitemController {

    @Autowired
    private BConstantitemService constantitemService;

    @Autowired
    private BConstanttypeService constanttypeService;

    @GetMapping("index")
    public String constatIndex(Model model){
        List<Constantitem> constantitems = constantitemService.list();
        model.addAttribute("consts",constantitems);

        return "constat-index";
    }

    @RequestMapping("del")
    @ResponseBody
    public Result delConstat(@RequestParam("id") Integer id) {

        Boolean falg  = constantitemService.removeById(id);
        if (falg){
            return Result.succ("删除成功！");
        }

        return Result.failure("删除失败！");
    }


    @GetMapping("edit")
    public String gotoEditPage(@RequestParam("id") Integer id, Model model) {

        Constantitem constantitem = constantitemService.getById(id);

        model.addAttribute("constantitem", constantitem);

        return "constat-add";
    }


    @GetMapping("add")
    public String gotoAddPage(Model model) {

        List<Constanttype> constanttypes = constanttypeService.list();

        model.addAttribute("constantitem", new Constantitem());
        model.addAttribute("constanttypes", constanttypes);

        return "constat-add";
    }

    @PostMapping("add")
    @ResponseBody
    public Result add(@RequestBody Constantitem constantitem){
        constantitem.setDelMark(1);
        Boolean falg = constantitemService.saveOrUpdate(constantitem);
        if (falg){
            return Result.succ("添加成功");
        }
        return Result.failure("添加失败");

    }

}

